package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: ljl
 * @Date: 2022/11/16/21:55
 * @Description:
 */
@Slf4j
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 查询敏感词
     * @param sensitiveDto
     * @return
     */
    @Override
    public PageResponseResult wmSensitiveList(SensitiveDto sensitiveDto) {
        //前端传来的page,size校验
        sensitiveDto.checkParam();

        IPage page = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper();
        //name不为0，则根据name模糊查询
        queryWrapper.like(sensitiveDto.getName()!=null,WmSensitive::getSensitives, sensitiveDto.getName());
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);

        page(page,queryWrapper);

        PageResponseResult result = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 根据id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult wmSensitiveDeleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id删除
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult wmSensitiveSave(WmSensitive wmSensitive){
        if(wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断添加的数据在数据库中存不存在
        WmSensitive sensitive = this.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if(sensitive==null){
            Date date=new Date();//获取一个java.util包下的Date对象
            Timestamp time=new Timestamp(date.getTime());//然后将时间转换成数据库类型的datetime类型
            wmSensitive.setCreatedTime(time);
            save(wmSensitive);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新敏感词
     */
    @Override
    public ResponseResult wmSensitiveUpdate(WmSensitive wmSensitive) {
        //判断参数是否为空
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询这条数据在数据库中存不存在，不存在则更新敏感词；存在，则返回提示信息
        WmSensitive sensitive = this.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive == null) {
            this.updateById(wmSensitive);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult getAll() {
        List<WmSensitive> sensitiveObjectList = wmSensitiveMapper.selectList(null);
        List<String> strList = sensitiveObjectList.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        return ResponseResult.okResult(strList);
    }
}
